package com.atguigu.mybatisplus.controller;

import com.atguigu.mybatisplus.common.result.Result;
import com.atguigu.mybatisplus.model.form.UserForm;
import com.atguigu.mybatisplus.model.query.UserPageQuery;
import com.atguigu.mybatisplus.model.vo.DataUserPageVO;
import com.atguigu.mybatisplus.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Tag(name = "用户接口")
@RestController
@RequestMapping("/api/v1/users")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户分页列表
     *
     * @return
     */
    @Operation(summary = "用户分页列表")
    @GetMapping("/page")
    public Result<DataUserPageVO> pageUser(@Valid @ParameterObject UserPageQuery userPageQuery) {
        DataUserPageVO dataUserPageVO = userService.getUserPage(userPageQuery);
        return Result.success(dataUserPageVO);
    }

    /**
     * 用户新增
     *
     * @param userForm
     * @return
     */
    @Operation(summary = "新增用户")
    @PostMapping("/save")
    @Validated
    public Result<String> saveUser(@Valid @RequestBody UserForm userForm) {
        userService.saveUser(userForm);
        return Result.success("新增成功");
    }
    /**
     * 用户删除
     * @param ids
     * @return
     */
    @Operation(summary = "删除用户")
    @DeleteMapping("/{ids}")
    public Result<String> deleteUser(@PathVariable List<Long> ids){
        userService.removeByIds(ids);
        return Result.success("删除成功");
    }
    /**
     * 用户更新
     * @param userId
     * @param userForm
     * @return
     */
    @Operation(summary = "修改用户")
    @PutMapping("/{userId}")
    public Result<String> updateUser(@PathVariable Long userId,@RequestBody @Valid UserForm userForm){
        userService.updateUserById(userId,userForm);
        return Result.success("修改成功");
    }
    /**
     * 用户详情
     * @param userId
     * @return
     */
    @Operation(summary = "用户表单数据")
    @GetMapping("/{userId}/from")
    public Result<UserForm> getUserForm(@PathVariable Long userId){
        UserForm userForm = userService.getUserFormById(userId);
        return Result.success(userForm);
    }
    /**
     * 用户密码更新
     * @param userId
     * @param password
     * @return
     */
    @Operation(summary = "重置用户密码")
    @PatchMapping("/{userId}/password")
    public Result<String> updatePassword(@PathVariable Long userId,@RequestParam String password){
        userService.updatePassword(userId,password);
        return Result.success("重置密码成功");
    }
    /**
     * 用户状态更新
     * @param userId
     * @param status
     * @return
     */
    @Operation(summary = "更新用户状态")
    @PatchMapping("/{userId}/status")
    public Result<String> updateStatus(@PathVariable Long userId,@RequestParam Integer status){
        userService.updateStatus(userId,status);
        return Result.success("更新状态成功");
    }
}
